/**
 * Chinahometex communication javascript library
 * 
 * @author liuhao
 */
if (!chinahometex) {
	var chinahometex = {};
};

if (!chinahometex.cm) {
	chinahometex.cm = {};
};

if (!chinahometex.cm.util) {
	chinahometex.cm.util = {};
};

chinahometex.cm.util.StringBuffer = function(s) {
	Object.call(this);
	if (s && typeof (s) == 'string') {
		this.charsequence = s;
	}
};
chinahometex.cm.util.StringBuffer.prototype = new Object();
chinahometex.cm.util.StringBuffer.prototype.charsequence = null;
chinahometex.cm.util.StringBuffer.prototype.append = function(s) {
	this.charsequence += s;
	return this;
};
chinahometex.cm.util.StringBuffer.prototype.substring = function(s, e) {
	return new chinahometex.util.StringBuffer(this.toString().substring(s, e));
};

chinahometex.cm.util.StringBuffer.prototype.toString = function() {
	return this.charsequence;
};



chinahometex.cm.util.Entry = function(k, v) {
	this.key = k;
	this.value = v;
};

chinahometex.cm.util.Entry.prototype = new Object();

chinahometex.cm.util.Entry.prototype.key = null;

chinahometex.cm.util.Entry.prototype.value = null;

/**
 * Map object
 */
chinahometex.cm.util.Map = function(k,v) {
	this.repo = [];
	this.put(k,v);
};

chinahometex.cm.util.Map.prototype = new Object();

chinahometex.cm.util.Map.prototype.repo = null;

chinahometex.cm.util.Map.prototype.put = function(k,v)	{
	this.repo.push(new chinahometex.cm.util.Entry(k,v));
};

chinahometex.cm.util.Map.prototype.get = function(k) {
	for(var i=0; i< this.repo.length; i++) {
		var entry = this.repo[i];
		if(k == entry.key)	{
			return entry.value;
		}
	}
	return null;
}

chinahometex.cm.HttpClient = function() {
	this.parameterMap = new chinahometex.cm.util.Map();
	this.ajax = false;
	if(window.ActiveXObject) {
		this.ajax =  new ActiveXObject("microsoft.xmlhttp");
	}else {
		this.ajax = new XMLHttpRequest();
	}
	
	if(!this.ajax) {
		throw "Ajax Initialize Faild";
	}
};

chinahometex.cm.HttpClient.prototype = new Object();

chinahometex.cm.HttpClient.prototype.action = null;


chinahometex.cm.HttpClient.prototype.parameterMap = null;

chinahometex.cm.HttpClient.prototype.ajax = null;

chinahometex.cm.HttpClient.prototype.setParameter = function(pname, pvalue) {
    this.parameterMap.put(pname,pvalue);
};

chinahometex.cm.HttpClient.prototype.encode = function() {
	var buffer = new chinahometex.cm.util.StringBuffer(" ");
	buffer.append("<request>");
	var repo = this.parameterMap.repo;
	for(var i=0; i<repo.length; i++) {
		var entry = repo[i];
		var name = entry.key;
		var value = entry.value;
		
		buffer.append("<property name='").append(name).append("' value='");
		buffer.append(value).append("' />");
	}
	buffer.append("</request>");
	
	return buffer.toString();
};


chinahometex.cm.HttpClient.prototype.doPost = function(url,callback) {
	this.sendRequest("POST",url,callback);
};

chinahometex.cm.HttpClient.prototype.doGet = function(url,callback) {
	this.sendRequest("GET",url,callback); 
};

chinahometex.cm.HttpClient.prototype.sendRequest = function(mtd,url,callback) {
	this.ajax.open(mtd,url,true); 
	var proxy = this.ajax;
	this.ajax.onreadystatechange = function() {
		callback(proxy);
	};
	this.ajax.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	this.ajax.send("procedure="+this.action+"&request="+encodeURIComponent(this.encode()));
};

